package com.work.dao.impl;

import com.work.bean.Hw;
import com.work.dao.BaseDao;
import com.work.dao.HwDao;

import java.util.List;

public class HwDaoImpl extends BaseDao implements HwDao {
    public List<Hw> queryHw() {
        String sql = "SELECT * FROM t_hw ";
        return  queryForList(Hw.class,sql);
    }

    public Hw queryHwById(Integer id) {
        String sql = "SELECT * FROM t_hw WHERE id = ? ";
        return  queryForOne(Hw.class,sql,id);
    }

    @Override
    public List<Hw> queryHwByTeacherId(Integer teacher_id) {
        String sql = "SELECT * FROM t_hw WHERE teacher_id = ? ";
        return  queryForList(Hw.class,sql,teacher_id);
    }

    @Override
    public Hw queryHwByTime() {
        String sql = "SELECT * FROM t_hw ORDER BY id DESC LIMIT 1 ";
        return  queryForOne(Hw.class,sql);
    }

    public int saveHw(Hw hw) {
        String sql = "INSERT INTO `t_hw` (`teacher_id`, `title`, `describe`, `end_time`) VALUES (?, ?, ?, ?)";
        return update(sql,hw.getTeacher_id(),hw.getTitle(),hw.getDescribe(),hw.getEnd_time());
    }

    public int updateHw(Hw hw) {
        String sql = "UPDATE t_hw SET `teacher_id`=?,`title`=?,`describe`=?,`end_time`=? WHERE `id` = ?";
        return update(sql,hw.getTeacher_id(),hw.getTitle(),hw.getDescribe(),hw.getEnd_time(),hw.getId());
    }

    public int deleteHw(Integer id) {
        String sql = "DELETE FROM `t_hw` WHERE `id` = ?";
        return update(sql, id);
    }
}
